// 阿里云短信接口
// const Core = require('@alicloud/pop-core');
const jwt = require("jsonwebtoken");

// 腾讯短信接口
const tencentcloud = require("tencentcloud-sdk-nodejs");
const SmsClient = tencentcloud.sms.v20190711.Client;
module.exports = {
    sendMsg(tel, code) {
        // var client = new Core({
        //     accessKeyId: 'LTAI4GHsvUyWBB9YBMfHbHk6',
        //     accessKeySecret: 'Om5GaHNITnjphhehT0lh4uL5ASnXJ2',
        //     endpoint: 'https://dysmsapi.aliyuncs.com',
        //     apiVersion: '2017-05-25'
        // });
        const clientConfig = {
            credential: {
              secretId: "AKID5oLbaPUAzKWYSCrZJyYvW0GOsChLi8LZ",
              secretKey: "KgeNq2MPziBdopgV9PVRQtBd2iRya6MT",
            },
            region: "",
            profile: {
              httpProfile: {
                endpoint: "sms.tencentcloudapi.com",
              },
            },
        };
        

        // var params = {
        //     "RegionId": "cn-hangzhou",
        //     "PhoneNumbers": tel,
        //     "SignName": "爱code爱生活",
        //     "TemplateCode": "SMS_203728335",
        //     "TemplateParam": `{"code":${code}}`
        // }


        const client = new SmsClient(clientConfig);
        const params = {
            "PhoneNumberSet":['+86'+tel],
            "TemplateID": "881527",
            "Sign": "爱code爱生活",
            "TemplateParamSet": [code],
            "SmsSdkAppid": "1400489828"
        };

        // var requestOption = {
        //     method: 'POST'
        // };

        return new Promise((resolve,reject)=>{
            // client.request('SendSms', params, requestOption).then((result) => {
            //     resolve(JSON.stringify(result));
            // }, (ex) => {
            //     reject(ex);
            // })

            client.SendSms(params).then(
                (data) => {
                    resolve(data);
                },
                (err) => {
                    reject("error", err);
                }
            );
        })
    },
    verify(token){
        return new Promise((resolve, reject)=>{
            jwt.verify(token, "老子天下第一", (err, data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    }
}


// 发送短信工具的调用方式

// tools.sendMsg(17600901915, 6243).then((result)=>{
// }).catch((err)=>{
// })